package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.api.OrganizationApi;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.mapper.PoliceWarningsMapper;
import com.smile.model.domain.FeisuanPoliceWarnings;
import com.smile.model.dto.request.AddPoliceWarningsRequestDTO;
import com.smile.model.dto.request.EditPoliceWarningsRequestDTO;
import com.smile.model.dto.request.GetPoliceWarningsDetailsRequestDTO;
import com.smile.model.dto.request.GetPoliceWarningsListRequestDTO;
import com.smile.model.response.response.GetPoliceWarningsDetailsResponseDTO;
import com.smile.model.response.response.GetPoliceWarningsListResponseDTO;
import com.smile.model.request.GetOrganizationByIdRequest;
import com.smile.model.response.GetOrganizationByIdResponse;
import com.smile.service.PoliceWarningsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PoliceWarningsServiceImpl implements PoliceWarningsService {
    @Autowired
    private PoliceWarningsMapper policeWarningsMapper;

    @Autowired
    private OrganizationApi organizationApi;

    @Override
    public Page<GetPoliceWarningsListResponseDTO> getPoliceWarningsList(GetPoliceWarningsListRequestDTO getPoliceWarningsListRequestDTO) {
        QueryWrapper<FeisuanPoliceWarnings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(getPoliceWarningsListRequestDTO)
                && StrUtil.isNotBlank(getPoliceWarningsListRequestDTO.getPoliceWarningsName()), "police_warning_name", getPoliceWarningsListRequestDTO.getPoliceWarningsName());

        queryWrapper.eq(ObjectUtil.isNotNull(getPoliceWarningsListRequestDTO) && ObjectUtil.isNotNull(getPoliceWarningsListRequestDTO.getState())
                , "state", getPoliceWarningsListRequestDTO.getState());
        Page<FeisuanPoliceWarnings> feisuanPoliceWarnings = policeWarningsMapper.selectPage(new Page<>(getPoliceWarningsListRequestDTO.getCurrentPage(), getPoliceWarningsListRequestDTO.getPageSize()), queryWrapper);
        Page<GetPoliceWarningsListResponseDTO> getPoliceWarningsListResponseDTOPage = MyBatisPlusPageConverter.convertPage(feisuanPoliceWarnings, GetPoliceWarningsListResponseDTO.class);
        List<GetPoliceWarningsListResponseDTO> collect = feisuanPoliceWarnings.getRecords().stream().map(item -> {
            GetPoliceWarningsListResponseDTO getPoliceWarningsListResponseDTO = BeanUtil.copyProperties(item, GetPoliceWarningsListResponseDTO.class);
            switch (item.getLevel()) {
                case 0:
                    getPoliceWarningsListResponseDTO.setLevel("一般");
                    break;
                case 1:
                    getPoliceWarningsListResponseDTO.setLevel("重要");
                    break;
            }

            switch (item.getState()) {
                case 0:
                    getPoliceWarningsListResponseDTO.setState("待执行");
                    break;
                case 1:
                    getPoliceWarningsListResponseDTO.setState("执行中");
                    break;
                case 2:
                    getPoliceWarningsListResponseDTO.setState("已完成");
                    break;
                case 3:
                    getPoliceWarningsListResponseDTO.setState("已终止");
                    break;
                case 4:
                    getPoliceWarningsListResponseDTO.setState("已取消");
                    break;
            }
            GetOrganizationByIdRequest getOrganizationByIdRequest = new GetOrganizationByIdRequest();
            getOrganizationByIdRequest.setId(item.getOrganizationId());

            R<GetOrganizationByIdResponse> organizationById = organizationApi.getOrganizationById(getOrganizationByIdRequest);
            if (ObjectUtil.isNotNull(organizationById) || ObjectUtil.isNotNull(organizationById.getData())) {
                getPoliceWarningsListResponseDTO.setOrganizationName(organizationById.getData().getOrganizationName());
            }

            return getPoliceWarningsListResponseDTO;
        }).collect(Collectors.toList());
        getPoliceWarningsListResponseDTOPage.setRecords(collect);


        return getPoliceWarningsListResponseDTOPage;
    }

    @Override
    public GetPoliceWarningsDetailsResponseDTO getPoliceWarningsDetails(GetPoliceWarningsDetailsRequestDTO getPoliceWarningsDetailsRequestDTO) {
        FeisuanPoliceWarnings feisuanPoliceWarnings = policeWarningsMapper.selectById(getPoliceWarningsDetailsRequestDTO.getId());
        GetPoliceWarningsDetailsResponseDTO getPoliceWarningsDetailsResponseDTO = BeanUtil.copyProperties(feisuanPoliceWarnings, GetPoliceWarningsDetailsResponseDTO.class);

        switch (feisuanPoliceWarnings.getLevel()) {
            case 0:
                getPoliceWarningsDetailsResponseDTO.setLevel("一般");
                break;
            case 1:
                getPoliceWarningsDetailsResponseDTO.setLevel("重要");
                break;
        }

        switch (feisuanPoliceWarnings.getState()) {
            case 0:
                getPoliceWarningsDetailsResponseDTO.setState("待执行");
                break;
            case 1:
                getPoliceWarningsDetailsResponseDTO.setState("执行中");
                break;
            case 2:
                getPoliceWarningsDetailsResponseDTO.setState("已完成");
                break;
            case 3:
                getPoliceWarningsDetailsResponseDTO.setState("已终止");
                break;
            case 4:
                getPoliceWarningsDetailsResponseDTO.setState("已取消");
                break;
        }

        GetOrganizationByIdRequest getOrganizationByIdRequest = new GetOrganizationByIdRequest();
        getOrganizationByIdRequest.setId(feisuanPoliceWarnings.getOrganizationId());

        R<GetOrganizationByIdResponse> organizationById = organizationApi.getOrganizationById(getOrganizationByIdRequest);
        if (ObjectUtil.isNotNull(organizationById) || ObjectUtil.isNotNull(organizationById.getData())) {
            getPoliceWarningsDetailsResponseDTO.setOrganizationName(organizationById.getData().getOrganizationName());
        }

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("timestamp", LocalDateTime.now());
        map.put("content", "任务下发");
        list.add(map);
        getPoliceWarningsDetailsResponseDTO.setTaskExecuteSituation(list);

        return getPoliceWarningsDetailsResponseDTO;
    }

    @Override
    public Boolean addPoliceWarnings(AddPoliceWarningsRequestDTO addPoliceWarningsRequestDTO) {
        FeisuanPoliceWarnings feisuanPoliceWarnings = BeanUtil.copyProperties(addPoliceWarningsRequestDTO, FeisuanPoliceWarnings.class);
        feisuanPoliceWarnings.setState(0);
        feisuanPoliceWarnings.setCreateTime(LocalDateTime.now());
        policeWarningsMapper.insert(feisuanPoliceWarnings);
        return Boolean.TRUE;
    }

    @Override
    public Boolean editPoliceWarnings(EditPoliceWarningsRequestDTO editPoliceWarningsRequestDTO) {
        UpdateWrapper<FeisuanPoliceWarnings> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(ObjectUtil.isNotNull(editPoliceWarningsRequestDTO)
                && ObjectUtil.isNotNull(editPoliceWarningsRequestDTO.getId()), "id", editPoliceWarningsRequestDTO.getId());
        updateWrapper.set(ObjectUtil.isNotNull(editPoliceWarningsRequestDTO) || ObjectUtil.isNotNull(editPoliceWarningsRequestDTO.getId()), "state", editPoliceWarningsRequestDTO.getState());

        policeWarningsMapper.update(null, updateWrapper);

        return Boolean.TRUE;
    }
}
